package com.service.school.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.base.BaseDaoI;
import com.model.TStudent;
import com.pageModel.DataGrid;
import com.pageModel.Student;
import com.service.school.StudentServiceI;

@Service(value = "studentService")
public class StudentServiceImpl implements StudentServiceI {

	private BaseDaoI<TStudent> studentDao;

	public BaseDaoI<TStudent> getStudentDao() {
		return studentDao;
	}

	@Autowired
	public void setStudentDao(BaseDaoI<TStudent> studentDao) {
		this.studentDao = studentDao;
	}

	@Override
	public DataGrid datagrid(Student student) {
		DataGrid dg = new DataGrid();
		String hql = "from TStudent t where 1=1 ";
		if (student.getStuId() != null && !student.getStuId().trim().equals("")) {
			hql += "and t.stuId like '%" + student.getStuId() + "%'";
		}
		if (student.getStuName() != null && !student.getStuName().trim().equals("")) {
			hql += "and t.stuName like '%" + student.getStuName() + "%'";
		}
		String totalHql = "select count(*) " + hql;
		List<TStudent> l = studentDao.find(hql, student.getPage(), student.getRows());
		List<Student> nl = new ArrayList<Student>();
		if (l != null && l.size() > 0) {
			for (TStudent t : l) {
				Student u = new Student();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
		dg.setTotal(studentDao.count(totalHql));
		dg.setRows(nl);
		return dg;
	}

	@Override
	public boolean add(Student student) {
		Long count = studentDao.count("select count(*) from TStudent obj where obj.stuId=?", new Object[] {student.getStuId()});
		if (count != null && count > 0) {
			 return  false;
		}
		TStudent t = new TStudent();
		if ((null != student.getStuId() || !student.getStuId().trim().equals("")) && (null != student.getStuName() || !student.getStuName().trim().equals(""))) {
			t.setId(UUID.randomUUID().toString());
			t.setStuId(student.getStuId());
			t.setStuName(student.getStuName());
			t.setStuSex(student.getStuSex().trim());
			t.setBornDate(student.getBornDate());
			t.setClassName(student.getClassName());
			t.setIsSpecial("否");
			if (isAddInfo(student.getHomeTel())) {
				t.setHomeTel(student.getHomeTel());
			}
			if (isAddInfo(student.getIdentityCard())) {
				t.setIdentityCard(student.getIdentityCard());
			}
			if (isAddInfo(student.getPhone())) {
				t.setPhone(student.getPhone());
			}
			if (isAddInfo(student.getAddress())) {
				t.setAddress(student.getAddress());
			}
		}
		studentDao.saveOrUpdate(t);
		return true;
	}

	private boolean isAddInfo(String getInfo) {
		if (null != getInfo || !getInfo.trim().equals("")) {
			return true;
		} else {
			return false;
		}
	}

	private boolean isAddInfo(Date getInfo) {
		if (null != getInfo || !getInfo.equals("")) {
			return true;
		} else {
			return false;
		}
	}

	private boolean isAddInfo(Integer getInfo) {
		if (null != getInfo) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete TStudent t where t.id in(";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		studentDao.executeHql(hql);

	}
	@Override
	public void edit(Student student) {
//		Long count = studentDao.count("select count(*) from TStudent obj where obj.id=?", new Object[] {student.getId()});
//		if (count != null && count > 0) {
//			 return  false;
//		}
		TStudent t=studentDao.get(TStudent.class,student.getId());
		BeanUtils.copyProperties(student, t);
	}
	@Override
	public void editSpecial(Student student) {
		TStudent t=studentDao.get(TStudent.class,student.getId());
		t.setIsSpecial("是");
		studentDao.update(t);
	}

}
